# -*- coding: utf-8 -*-

from django.http import JsonResponse
from django.db.models import Q

from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination

from link.models import Link
from link.serializers import LinkSerializer


class LinkPagination(PageNumberPagination):
    '''自定义分页
    '''
    #默认每页显示的个数
    page_size = 20
    #可以动态改变每页显示的个数
    page_size_query_param = 'page_size'
    #页码参数
    page_query_param = 'page'
    #最多能显示多少页
    max_page_size = 100


class LinkListView(APIView):
    def get(self, request, format=None):
        q = Q(display="True")
        my_dict = {}
        q1 = Link.objects.filter(q).order_by('date')
        paginate = LinkPagination()
        page_obj = paginate.paginate_queryset(q1, request)           
        serializer = LinkSerializer(page_obj, many=True)
        return JsonResponse({"count": q1.count(), "results": serializer.data})
 
 
def show_link_num(request):
    num = Link.objects.filter(Q(display="True")).count()
    return JsonResponse({'num_link': num}, safe=False)



